# 1.用正则来爬取(https://image.baidu.com/)
#    <1>要求搜索什么关键字则爬相对应的图片(例如:白丝 黑丝)
#    <2>将所有的图片存储到`关键字`文件夹中, 该文件夹是通过代码实现的
#    <3>图片的名字可以随意取
#    <4>交代码即可
# 案例:航天员
import os

import re

import requests

from lxml import etree


def baidu(page, word):
    # 向网址发出请求
    url = f"https://image.baidu.com/search/index?tn=baiduimage&word={word}&pn={page * 60}"
    # 模拟浏览器
    headers = {

        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36',

        'Cookie': 'BDqhfp=NBA%26%260-10-1undefined%26%260%26%261; BIDUPSID=7B6AB0BC3972CDDBF9F254075DB55581; PSTM=1639724122; __yjs_duid=1_65bded486a4fd877358a2b67fe59e4181639726446468; BAIDUID=8C4C34E310A72516E50E4B6D625D49ED:SL=0:NR=10:FG=1; BDUSS=zBTaHBNZEl6b0EycEdjelF1VHEtQzFhQk1naVpGeHYwd0paZUdSUmJnY0ZTc2xqSUFBQUFBJCQAAAAAAAAAAAEAAADYJkeRX1~Q7MDPz8jJ-gAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAW9oWMFvaFjb; BDUSS_BFESS=zBTaHBNZEl6b0EycEdjelF1VHEtQzFhQk1naVpGeHYwd0paZUdSUmJnY0ZTc2xqSUFBQUFBJCQAAAAAAAAAAAEAAADYJkeRX1~Q7MDPz8jJ-gAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAW9oWMFvaFjb; H_WISE_SIDS=219946_231979_222624_234044_219623_232219_237836_232777_236434_235174_239363_239479_234020_237526_234208_231498_219564_234295_238204_237364_234426_234306_235441_235513_236239_240365_238755_240305_236537_235714_236653_240881_240447_240465_240725_241232_241207_241177_240782_241249_240598_240650_241296_239947_239492_240034_238301_238226_216839_224267_227932_213348_229968_241197_211986_239900_223323_215727_237892_214798_234834_240025_223064_219942_238506_213045_228650_229154_226628_237819_238036_241246_241567_238072_240905_188333_230288_232628_241715_241753_241758_239086_241846_241810_241872_240966_241958_240204_242024_240734_241780_232630_241373_242219_242383_242372_232321_241600_241688_241695_242476_222222_242517_237964_242548; H_WISE_SIDS_BFESS=219946_231979_222624_234044_219623_232219_237836_232777_236434_235174_239363_239479_234020_237526_234208_231498_219564_234295_238204_237364_234426_234306_235441_235513_236239_240365_238755_240305_236537_235714_236653_240881_240447_240465_240725_241232_241207_241177_240782_241249_240598_240650_241296_239947_239492_240034_238301_238226_216839_224267_227932_213348_229968_241197_211986_239900_223323_215727_237892_214798_234834_240025_223064_219942_238506_213045_228650_229154_226628_237819_238036_241246_241567_238072_240905_188333_230288_232628_241715_241753_241758_239086_241846_241810_241872_240966_241958_240204_242024_240734_241780_232630_241373_242219_242383_242372_232321_241600_241688_241695_242476_222222_242517_237964_242548; indexPageSugList=%5B%22%E9%A3%8E%E4%BA%AC%E5%9B%BE%E7%89%87%22%2C%2212.%22%2C%22%E6%B8%94%E7%BD%91%E8%A2%9C%22%2C%22%E9%B1%BC%E7%BD%91%22%2C%22%E5%9B%BE%E7%89%87%22%2C%22NBA%22%2C%22%E7%B2%89%E4%B8%9D%22%2C%22%E7%99%BD%E4%B8%9D%22%2C%22%E5%A4%A9%E7%9C%BC%E6%9F%A5%22%5D; BAIDUID_BFESS=8C4C34E310A72516E50E4B6D625D49ED:SL=0:NR=10:FG=1; delPer=0; PSINO=5; BA_HECTOR=0kal81al012g252g8k2h2lqn1hv640f1l; ZFY=xUXD2e5YdTMnUOcKw8GKPLX38fpYqGytnn9ltuD1Zf0:C; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; H_PS_PSSID=38186_36551_38094_38132_37911_38149_38175_38171_38224_37924_38089_38040_26350_38120_38008_37881; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BDRCVFR[X_XKQks0S63]=mk3SLVN4HKm; firstShowTip=1; BDRCVFR[Q5XHKaSBNfR]=mk3SLVN4HKm; BDRCVFR[dG2JNJb_ajR]=mk3SLVN4HKm; userFrom=null; BDRCVFR[-pGxjrCMryR]=mk3SLVN4HKm; ab_sr=1.0.1_NDJjY2ZiNzFiMTFlYTM3ZTNmY2VlNTE1ZWYwZDExOGJkODdiNjFhNDkyYzY3OTU3NzQ5Njc1ODM5MGI3ZTIzNDEwMzY4NjAzYWEyYWQ0MGRkMzAzN2ZlZTEzYTBhMzhkZjE4OWU4MWQ1Yzg1OWQxOWJmZGU0YWIzZDkyMzBiN2FhZTM0NjkxMzI2YWY0MmFhMzJjZWVhYzBjNWY5MDg4Mg=='
    }
    # 获取源代码
    r = requests.get(url, headers=headers).text
    # 正则获取所有的img标签
    img_list = re.findall('"thumbURL":"(.*?)"', r)
    # 写入循环
    i = 1
    for img in img_list:
        img = img.replace('\\', '')
        try:
            img_code = requests.get(img, headers=headers).content
        except:
            continue
        # 保存图片,写入二进制数据
        with open(f'{word}/{page}-{i}.png', 'wb') as f:
            f.write(img_code)
        print(f'{page}-{i}保存成功')
        i += 1

if __name__ == '__main__':
    word = input('请输入你想要的图片: ')
    if not os.path.exists(word):
        os.mkdir(word)
    for page in range(1, 11):
        baidu(page, word)